import { getData } from '@vimi/utils-api'
const state = {
  cfcaUserId: '',
}

const getters = {
  cfcaUserId: state => state.cfcaUserId,
}

const actions = {
  async getCfca({ commit, rootGetters }, force = false) {
    if (!force && state.cfcaUserId) {
      return state.cfcaUserId
    }
    const params = { 'service_id.eq': rootGetters.org.clientId }
    let data = {}
    await getData('credit', 'cfca_account', params)
      .then(res => {
        data = (res && res.data && res.data[0]) || {}
        commit('setUserId', data.user_id)
        return data.user_id || ''
      })
      .catch(() => {
        return ''
      })
  },
}

const mutations = {
  setUserId: (state, payload) => (state.cfcaUserId = payload),
}

export default {
  state,
  getters,
  actions,
  mutations,
}
